package com.zzx.auth.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.stereotype.Component;

/**
 * 记录登录日志
 */
@Slf4j
@Component
public class AuthenticationSuccessEventListener implements ApplicationListener<AuthenticationSuccessEvent> {

    /**
     * 重新授权成功事件
     *
     * @param event
     */
    @Override
    public void onApplicationEvent(AuthenticationSuccessEvent event) {
        // 登录成功后的事件 记录登录日志....
        System.out.println(event);
        Object result = event.getSource();
        if (result != null && result instanceof UsernamePasswordAuthenticationToken) {
            UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) result;
            log.info("{} 登录成功!", token.getName());
        }

    }
}
